package com.example.service;

import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    String TAG="MainActivity";
    boolean isServiceBinded;
   ICommucation remoteBinder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d(TAG,"oncreate");
    }

   public void startServiceClick(View view)
    {
        Intent intent=new Intent();
        intent.setClass(this,FirstService.class);
        startService(intent);
    }

 public void stopService(View view)
{
    Intent intent=new Intent();
    intent.setClass(this,FirstService.class);
    stopService(intent);
}

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy..");
    }



    public void bindServiceClick(View view) {
        Intent intent = new Intent();
        intent.setClass(this, FirstService.class);
 isServiceBinded=bindService(intent, mConnection, BIND_AUTO_CREATE);


        }
        private ServiceConnection mConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
 Log.d(TAG,"onServiceConnection");
                remoteBinder=(ICommucation) iBinder;
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
Log.d(TAG,"onServiceDisconnected");
remoteBinder=null;
            }

    };

    public void unbindServiceClick(View view) {
    if(mConnection!=null&&isServiceBinded)
    {
        unbindService(mConnection);
    }

    }

    public void callServcieMethod(View view) {
        Log.d(TAG,"call service inner method");
        remoteBinder.callServiceInnerMethod();

    }
}